package com.byl.modules.core.controller;


import com.byl.common.modules.controller.BaseController;
import com.byl.modules.core.service.ProductService;
import com.byl.sdk.bean.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/product")
public class ProductController extends BaseController {

    @Autowired
    ProductService productService;

    @PostMapping("/list")
    public String query(@RequestBody Map map) {
        ResultBean bean = productService.query(map);
        return success("产品列表", bean);
    }

   @PostMapping("/save")
    public String save(@RequestBody Map map) {
        int rs = productService.save(map);
        return rs > 0 ? success("保存成功!") : fail("保存失败!");
    }

    @PostMapping("/update")
    public String update(@RequestBody Map map, HttpServletRequest request) {
        // 获取服务器路径
        String root = request.getServletContext().getRealPath("/");
        int rs=productService.update(map,root);
        return rs > 0 ? success("修改成功!") : fail("修改失败!");
    }

    @GetMapping("/delete")
    public String delete(String id) {
        int rs=productService.delete(id);
        return rs>0? success("删除成功!"):fail("删除失败");
    }

    @GetMapping("/online")
    public String online(String id) {
        int rs = productService.updateStatus(id,1);
        return rs > 0 ? success("上线成功!") : fail("上线失败!");
    }

    @GetMapping("/offline")
    public String offline(String id) {
        int rs = productService.updateStatus(id,0);
        return rs > 0 ? success("下线成功!") : fail("下线失败!");
    }
}
